from django.db import models
from django.contrib.auth.models import User
import datetime

class Message(models.Model):
    sender = models.ForeignKey(User, related_name='sended_message_set')
    receiver = models.ForeignKey(User, related_name='received_message_set')
    title = models.CharField(max_length=100)
    content = models.TextField()
    post_time = models.DateTimeField(default=datetime.datetime.now())
    is_read = models.BooleanField(default=False)
    is_deleted = models.SmallIntegerField(default=0)  #for user to user message: 0-not yet delete, 1-reciver delete, 2-sender delete
    type = models.SmallIntegerField(default=0)  #0-user to user, 1-system

    class Meta:
        db_table = 'message'
    
    def sender_delete(self):
        if self.is_deleted == 0:
            self.is_deleted = 2
            self.save()
        elif self.is_deleted == 1:
            self.delete()
    
    def receiver_delete(self):
        if self.is_deleted == 0:
            self.is_deleted = 1
            self.save()
        elif self.is_deleted == 2:
            self.delete()